﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;

namespace ARC.Yunpian.SMSService.Service
{
    public class BaseYunpian
    {
        protected string Post(string url, string paramsDictionary)
        {
            var hanDer = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip,
            };
            using (HttpClient client = new HttpClient(hanDer))
            {
                try
                {
                    HttpContent httpContent =new StringContent(Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(paramsDictionary)));
                    httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
                    httpContent.Headers.ContentLength = paramsDictionary.Length;
                   
                    var response = client.PostAsync(url,httpContent).Result; //创建响应对象
                    string json = response.Content.ReadAsStringAsync().Result; //响应的结果
                    return json;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
      
            }
        }
        //protected string Post(string url, Dictionary<string,string> paramsDictionary)
        //{
        //    var hanDer = new HttpClientHandler()
        //    {
        //        AutomaticDecompression = DecompressionMethods.GZip,
        //    };
        //    using (HttpClient client = new HttpClient(hanDer))
        //    {
        //        try
        //        {
        //            byte[] byteArray = Encoding.Default.GetBytes(paramsDictionary); //转化 /
        //            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
        //            webReq.Method = "POST";
        //            webReq.ContentType = "application/x-www-form-urlencoded";

        //            webReq.ContentLength = byteArray.Length;
        //            Stream newStream = webReq.GetRequestStream();
        //            newStream.Write(byteArray, 0, byteArray.Length);//写入参数
        //            newStream.Close();
        //            HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
        //            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        //            ret = sr.ReadToEnd();
        //            sr.Close();
        //            response.Close();
        //            newStream.Close();
        //            //Dictionary<string, string> aa = new Dictionary<string, string>();
        //            //HttpContent httpContent =new StringContent(paramsDictionary);
        //            //httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
        //            //var response = client.PostAsync(url, httpContent).Result; //创建响应对象
        //            //string json = response.Content.ReadAsStringAsync().Result; //响应的结果
        //            //return json;
        //        }
        //        catch (Exception e)
        //        {
        //            Console.WriteLine(e);
        //            throw;
        //        }

        //    }
        //}
    }
}
